<!DOCTYPE html>
<script src="../../../../../resources/testharness.js"></script>
<script src="../../../../../resources/testharnessreport.js"></script>
<script src="../../../../../resources/gesture-util.js"></script>
<div id="testdiv" style="margin: 0px; width: 100px; height: 100px; background-color: blue;"></div>
<script>

var TEST_X = 40;
var TEST_Y = 70;
var TEST_SCALE = 0.5;
var TOLERANCE = 0.001;
var MOUSE_INPUT = GestureSourceType.MOUSE_INPUT;

var t = async_test("Touchpad pinch offers synthetic wheel events");

var numWheelEventsSeen = 0;
var totalScaleChange = 1.0;

function wheelHandler(event) {
  numWheelEventsSeen++;

  assert_true(event.ctrlKey);
  assert_approx_equals(event.deltaX, 0, TOLERANCE);
  assert_approx_equals(event.x, TEST_X, TOLERANCE);
  assert_approx_equals(event.y, TEST_Y, TOLERANCE);

  var scaleChange = Math.exp(-event.deltaY / 100);
  totalScaleChange *= scaleChange;
}

document.getElementById('testdiv').addEventListener(
    'wheel', t.step_func(wheelHandler));

if (window.chrome && chrome.gpuBenchmarking) {
  window.onload = t.step_func(function() {
    // Ensure the compositor is made aware of the handler before we send the
    // pinch.
    waitForCompositorCommit().then(t.step_func(function() {
      // Ensure that a touchpad pinch generates synthetic wheel event(s).
      chrome.gpuBenchmarking.pinchBy(
          TEST_SCALE, TEST_X, TEST_Y, t.step_func_done(function() {
        assert_greater_than(numWheelEventsSeen, 0);
        assert_approx_equals(totalScaleChange, TEST_SCALE, TOLERANCE);
      }), undefined /* speed */, MOUSE_INPUT);
    }));
  });
} else {
  t.step(function() {
    assert_unreached('This test requires chrome.gpuBenchmarking.pinchBy');
  });
  t.done();
}

</script>
